//
//  ReadDetailViewController.m
//  Slice
//
//  Created by lanou on 16/6/17.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "ReadDetailViewController.h"

@interface ReadDetailViewController ()

@end

@implementation ReadDetailViewController

- (void)getData {
    [RequestTool requsetWithType:POST URLString:kDetailURL paremeter:@{@"contentid":self.contentid} callBack:^(NSData *data, NSError *error) {

        if (data.length == 0) {
            return ;
        }
//        解析数据
        NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
   
        dispatch_async(dispatch_get_main_queue(), ^{

            //        数据请求成功才创建webView
            UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight -64)];
            
//            工程目录报地址
            /*
              强大的HTML + CSS 很强,但只有HTML 无法强大,如果我们只记得让 webView 加载HTML无法布局,不适配屏幕,我们需要给他制定一个CSS让他去布局
             和直接加载网页不同,网页自带CSS 多以不需要我们去指定
             */

            NSString *cssPath = [[NSBundle mainBundle] resourcePath];;
            
            //        加载HTML 内容
            [web loadHTMLString:jsonData[@"data"][@"html"] baseURL:[NSURL fileURLWithPath:cssPath]];
            
            [self.view addSubview:web];
        });
    }];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
    
//    添加按钮
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"分享"] style:UIBarButtonItemStylePlain target:self action:@selector(share)];
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"评论1"] style:UIBarButtonItemStylePlain target:self action:@selector(comment)];
    
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"收藏"] style:UIBarButtonItemStylePlain target:self action:@selector(store)];
    
    self.navigationItem.rightBarButtonItems = @[item, item1,item2];

    [[ReadDataBase1 share] howMuchTables];
}


//收藏按钮的方法
- (void)store {
    [[ReadDataBase1 share] creatDataBaseWithTableName:[NSString stringWithFormat:@"(name text premary key,model blob)"]];
    
    self.storeView = [[[NSBundle mainBundle] loadNibNamed:@"StoreSuccessView" owner:self options:nil]lastObject];
    [self.view addSubview:self.storeView];
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(storeViewHidden) userInfo:nil repeats:NO];
    
    ListMdel *model = [[ReadDataBase1 share] selectWithName:self.model.title];
    if (model == nil) {
        if ([[ReadDataBase1 share] addModel:self.model]) {
            self.storeView.showLable.text = @"收藏成功"
            ;
        } else {
            self.storeView.showLable.text = @"收藏失败"
            ;
        }
    } else {
        self.storeView.showLable.text = @"已收藏";
    }  
}

//收藏提示页面隐藏功能
- (void)storeViewHidden {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.storeView removeFromSuperview];
    });
}





// 评论的方法
- (void)comment {
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"user"] == nil) {
        UINavigationController *navc = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"navagation"];
        [self presentViewController:navc animated:YES completion:nil];
    } else {
        ReadDetaliCommentViewController *vc = [[ReadDetaliCommentViewController alloc] init];
        vc.contentId = self.contentid;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//分享
- (void)share {
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






@end
